import numpy as np
import openpyxl as pd
from datetime import datetime, time
from  random import *
import requests
import json

year = 2025
month = 5
is_summer =True if month>=5 and month <10 else False
week_index=0
date_index=[]
workday = json.loads(requests.get('https://api.jiejiariapi.com/v1/workdays/'+str(year)).text)
def read(sheet):
    global date_index,last_cell,week_index
    for row in sheet:
        for index in range(0,len(row)):
            if isinstance(row[index-1].value,str) and row[index-1].value=='name：':
                row[index].value = 'username'
            elif isinstance(row[index-1].value,str) and row[index-1].value=='job：':
                row[index].value = 'department'
            elif isinstance(row[index-1].value,str) and str.find(row[index-1].value,"星期")!=-1:
                if len(row[index-1].value)>2 and str(datetime.now().year)+'-{:02d}'.format(month)+'-{:02d}'.format(row[index-2].value) in workday:
                    row[index].value = "8:"+'{:02d}'.format(randint(30,59))
            elif isinstance(row[index-2].value,str) and str.find(row[index-2].value,"星期")!=-1:
                if len(row[index-2].value)>2 and str(datetime.now().year)+'-{:02d}'.format(month)+'-{:02d}'.format(row[index-3].value) in workday:
                    if not isinstance(row[index],pd.cell.MergedCell): 
                        row[index].value = "18:"+'{:02d}'.format(randint(0,30)) if is_summer else "17:"+'{:02d}'.format(randint(30,59))
            elif isinstance(row[index-3].value,str) and str.find(row[index-3].value,"星期")!=-1 and index >5:
                if len(row[index-3].value)>2 and str(datetime.now().year)+'-{:02d}'.format(month)+'-{:02d}'.format(row[index-4].value) in workday:
                    row[index].value = "18:"+'{:02d}'.format(randint(0,30)) if is_summer else "17:"+'{:02d}'.format(randint(30,59))

table = pd.load_workbook(r"D:\2025.1-12月考勤表.xlsx")
sheet=table['2025.'+str(month)]
read(sheet)
table.save(r'D:\2025.1-12月考勤表_yourname.xlsx')
print("save ok")